

#include "file_scripter.h"
#include <stdio.h>
#include <stdlib.h>
#include "log.h"


void FileScripter::run(Http* http, const std::string& localfile, const Http::Param& para) const
{
    FILE * fp = fopen(localfile.c_str(), "rb");
    if (!fp) {
        http->Reply404();
        return;
    }
    fseek(fp, 0, SEEK_END);
    int len = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    char * buffer = (char *)malloc(len);
    int total = 0;
    while (total < len) {
        int ret = fread(buffer + total, 1, len - total, fp);
        if (ret <= 0)
            break;
        total += ret;
    }
    fclose(fp);
    http->Reply(200, buffer, total);
    free(buffer);
}



